package com.zrf.base.knowledge.guava;

import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

/**
 * @author zhouruifeng
 *         Created by zhouruifeng on 2017/7/17.
 */
public class OrderingTest {
    public static void main(String[] args) {
        System.out.println(Ordering.natural().compare(1, 2)); //-1
        System.out.println(Ordering.natural().reverse().compare(1, 2));//1

        System.out.println(Ordering.usingToString().compare(2, 11));//1

        System.out.println(Ordering.from(String::compareTo).compare("2", "11"));//1

        System.out.println(Ordering.natural().greatestOf(Lists.newArrayList(1, 10, 3, 15), 2));
        System.out.println(Ordering.natural().leastOf(Lists.newArrayList(1, 10, 3, 15), 2));
        System.out.println(Ordering.natural().sortedCopy(Lists.newArrayList(1, 10, 3, 15)));
        System.out.println(Ordering.arbitrary().sortedCopy(Lists.newArrayList(1, 10, 3, 15)));
        System.out.println(Ordering.natural().isOrdered(Lists.newArrayList(1, 10, 3, 15)));
        System.out.println(Ordering.natural().isOrdered(Lists.newArrayList(1, 3, 10, 15)));
        System.out.println(Ordering.natural().isStrictlyOrdered(Lists.newArrayList(1, 3, 10, 15)));

        System.out.println(Ordering.natural().isOrdered(Lists.newArrayList(
                User.builder().id(1).name("2").build(),
                User.builder().id(1).name("1").build(),
                User.builder().id(2).name("2").build()
        )));//false
    }

    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class User implements Comparable<User> {
        private Integer id;
        private String name;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public int compareTo(User o) {
            return Ordering.natural().onResultOf((User u)->u.getId()).compound((o1, o2)->o1.getName().compareTo(o2.getName())).compare(this, o);
        }
    }
}
